﻿/// <summary>
/// FileName: ParseError.cs
/// Author: Jiang Xiaolong
/// Created Time: 2015.05.12
/// Version: 1.0
/// Company: Sunnytech
/// Function: 解析错误信息处理类；
///
/// Changed By:
/// Modification Time:
/// Discription:
/// </summary>
using Expression.api;

namespace Expression.impl
{
	public class ParseError
	{
		private PositionLocate _pos;
		private string _message;
		private Severity _severity;

		//警告或错误
		public enum Severity
		{
			WARNING,
			ERROR
		}

		protected ParseError(PositionLocate pos, string message, Severity severity)
		{
			this._pos = pos;
			this._message = message;
			this._severity = severity;
		}

		/// <summary>
		/// 增加警告信息；
		/// </summary>
		/// <param name="pos"></param>
		/// <param name="msg"></param>
		/// <returns></returns>
		public static ParseError Warning(PositionLocate pos, string msg)
		{
			string message = msg;
			if (pos.GetLine() > 0)
			{
				message = "警告>>>行号：" + pos.GetLine() + "，字符序号：" + pos.GetPos() + ", 警告信息：" + msg;
			}
			return new ParseError(pos, message, Severity.WARNING);
		}

		/// <summary>
		/// 增加错误信息；
		/// </summary>
		/// <param name="pos"></param>
		/// <param name="msg"></param>
		/// <returns></returns>
		public static ParseError Error(PositionLocate pos, string msg)
		{
			string message = msg;
			if (pos.GetLine() > 0)
			{
				message = "错误>>>行号：" + pos.GetLine() + "，字符序号：" + pos.GetPos() + ", 警告信息：" + msg;
			}
			return new ParseError(pos, message, Severity.ERROR);
		}

		/// <summary>
		/// 获取位置信息；
		/// </summary>
		/// <returns></returns>
		public PositionLocate GetPosition()
		{
			return _pos;
		}

		/// <summary>
		/// 获取具体错误信息；
		/// </summary>
		/// <returns></returns>
		public string GetMessage()
		{
			return _message;
		}

		/// <summary>
		/// 是警告还是错误；
		/// </summary>
		/// <returns></returns>
		public Severity GetSeverity()
		{
			return _severity;
		}

		public override string ToString()
		{
			return "类型：" + _severity.ToString() + "，信息：" + _message;
		}

	}
}
